/*
 * LoginPanel.java
 *
 * Created on 15 de julio de 2008, 18:31
 */

package org.perfectday.dashboard.gui;

import java.awt.Event;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

/**
 *  
 * @author  Miguel Angel Lopez Montellano (alakat@gmail.com)
 */
public class LoginPanel extends javax.swing.JPanel {

    private  Roster roster;
    private XMPPConnection connection;
    private JDialog parent;
    /** Creates new form LoginPanel */
    public LoginPanel(Frame parent) { 
        initComponents();
        this.setFocusable(true);
    }

    public String getUser() {
      return this.tfLogin.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfLogin = new javax.swing.JTextField();
        pfPassword = new javax.swing.JPasswordField();
        bConnectar = new javax.swing.JButton();

        jLabel1.setText("Login");

        jLabel2.setText("Password");

        tfLogin.setText("miguel.developer.game");
        tfLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfLoginActionPerformed(evt);
            }
        });
        tfLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfLoginKeyPressed(evt);
            }
        });

        pfPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pfPasswordKeyPressed(evt);
            }
        });

        bConnectar.setText("Conectar");
        bConnectar.setSelected(true);
        bConnectar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bConnectarMouseClicked(evt);
            }
        });
        bConnectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConnectarActionPerformed(evt);
            }
        });
        bConnectar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bConnectarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                            .addComponent(tfLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(bConnectar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bConnectar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void bConnectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConnectarActionPerformed
// TODO add your handling code here:
    ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
    this.connection = new XMPPConnection(connConfig);
    try{
         this.connection.connect();
        this.connection.login(this.tfLogin.getText(),new String(this.pfPassword.getPassword()));
        Presence p = new Presence(Presence.Type.available);
        p.setMode(Presence.Mode.available);
        connection.sendPacket(p);
        this.roster = this.connection.getRoster();
        this.parent.setVisible(false);
        System.out.println("Rosters "+this.roster.getEntryCount());
    }catch(Exception ex){
        JOptionPane.showMessageDialog(this,"Connection Error", "Error en la conexión",JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
}//GEN-LAST:event_bConnectarActionPerformed

private void bConnectarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bConnectarKeyPressed
// TODO add your handling code here:
    if(evt.getKeyCode()==evt.VK_ENTER){
        ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
    this.connection = new XMPPConnection(connConfig);
    try{
         this.connection.connect();
        this.connection.login(this.tfLogin.getText(),new String(this.pfPassword.getPassword()));
        Presence p = new Presence(Presence.Type.available);
        p.setMode(Presence.Mode.available);
        connection.sendPacket(p);
        this.roster = this.connection.getRoster();
        this.parent.setVisible(false);
        System.out.println("Rosters "+this.roster.getEntryCount());
    }catch(Exception ex){
        JOptionPane.showMessageDialog(this,"Connection Error", "Error en la conexión",JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
    }
}//GEN-LAST:event_bConnectarKeyPressed

private void bConnectarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bConnectarMouseClicked
// TODO add your handling code here:
    ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
    this.connection = new XMPPConnection(connConfig);
    try{
         this.connection.connect();
        this.connection.login(this.tfLogin.getText(),new String(this.pfPassword.getPassword()));
        Presence p = new Presence(Presence.Type.available);
        p.setMode(Presence.Mode.available);
        connection.sendPacket(p);
        this.roster = this.connection.getRoster();
        this.parent.setVisible(false);
        System.out.println("Rosters "+this.roster.getEntryCount());
    }catch(Exception ex){
        JOptionPane.showMessageDialog(this,"Connection Error", "Error en la conexión",JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
}//GEN-LAST:event_bConnectarMouseClicked

private void tfLoginKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfLoginKeyPressed
// TODO add your handling code here:
    if(evt.getKeyCode()==evt.VK_ENTER){
        ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
        this.connection = new XMPPConnection(connConfig);
        try{
             this.connection.connect();
            this.connection.login(this.tfLogin.getText(),new String(this.pfPassword.getPassword()));
            Presence p = new Presence(Presence.Type.available);
            p.setMode(Presence.Mode.available);
            connection.sendPacket(p);
            this.roster = this.connection.getRoster();
            this.parent.setVisible(false);
            System.out.println("Rosters "+this.roster.getEntryCount());
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,"Connection Error", "Error en la conexión",JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }
}//GEN-LAST:event_tfLoginKeyPressed

private void pfPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pfPasswordKeyPressed
// TODO add your handling code here:
    if(evt.getKeyCode()==evt.VK_ENTER){
        ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
        this.connection = new XMPPConnection(connConfig);
        try{
             this.connection.connect();
            this.connection.login(this.tfLogin.getText(),new String(this.pfPassword.getPassword()));
            Presence p = new Presence(Presence.Type.available);
            p.setMode(Presence.Mode.available);
            connection.sendPacket(p);
            this.roster = this.connection.getRoster();
            this.parent.setVisible(false);
            System.out.println("Rosters "+this.roster.getEntryCount());
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,"Connection Error", "Error en la conexión",JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }
}//GEN-LAST:event_pfPasswordKeyPressed

    private void tfLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfLoginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfLoginActionPerformed
  /**/
    public XMPPConnection getConnection() {
        return connection;
    }

    public void setConnection(XMPPConnection connection) {
        this.connection = connection;
    }

    public JDialog getParent() {
        return parent;
    }

    public void setParent(JDialog parent) {
        this.parent = parent;
    }

    public Roster getRoster() {
        return roster;
    }

    public void setRoster(Roster roster) {
        this.roster = roster;
    }

    @Override
    public void requestFocus() {
        super.requestFocus();
        pfPassword.requestFocus();
    }

    public void setInitialInfo(String userInit, String passInit){
        this.tfLogin.setText(userInit);
        this.pfPassword.setText(passInit);
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bConnectar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField pfPassword;
    private javax.swing.JTextField tfLogin;
    // End of variables declaration//GEN-END:variables

}
